package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cib.ebank.CibMessage;

/**
 * 兴业B2C网关支付前台通知处理
 * 
 * @CibB2cFrontRes.java
 * @author zhengxue
 * @2014-11-21 上午10:24:23 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class CibB2cFrontRes extends BankResBase {
    private static Log logger = LogFactory.getLog(CibB2cFrontRes.class);

    @RequestMapping(value = "/CibB2cFrontRes")
    protected ModelAndView execute(HttpServletRequest request, HttpServletResponse response) {
        logger.error("--------------- CibB2cFrontRes bankback ---------------");
        return super.execute(request, response);
    }

    @Override
    protected BaseMessage buildMessage(HttpServletRequest request) {
        CibMessage message = new CibMessage();
        message.setFront(true);
        message.setBank(BankConstants.BANK_LOGO_CIB);
        message.setAmount(new BigDecimal(request.getParameter("pay_amt")));
        message.setBankOrderId(request.getParameter("ord_no"));
        // 银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
        // 银行支付成功返回信息
        message.setBankCode(request.getParameter("bank"));
        message.setMrchNo(request.getParameter("mrch_no"));
        message.setOrdNo(request.getParameter("ord_no"));
        message.setCur(request.getParameter("cur"));
        message.setOrdAmt(request.getParameter("ord_amt"));
        message.setPayAmt(request.getParameter("pay_amt"));
        message.setSno(request.getParameter("sno"));
        message.setMac(request.getParameter("mac"));
        return message;
    }

    @Override
    protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
        OrderInfo orderInfo = new OrderInfo();
        CibMessage message = (CibMessage) result.getModel("message");
        if (message != null) {
            // 只有支付成功，银行才通知
            orderInfo.setOrderId(message.getOrderId());
            orderInfo.setAmt(message.getAmount().toString());
            orderInfo.setNotifyNum("1");
            orderInfo.setPayStatus("00");
            orderInfo.setRetCode("00");
            orderInfo.setRetMsg("");
        }
        return orderInfo;
    }

    @Override
    protected boolean checkParameters(HttpServletRequest request) {
        String mac = request.getParameter("mac");
        String ordNo = request.getParameter("ord_no");
        // 返回的密文或订单号有一项不存在，直接返回错误页面。
        if (StringUtils.isEmpty(mac) || StringUtils.isEmpty(ordNo)) {
            return false;
        }
        return true;
    }
}
